<template>
  <div class="container">
    <div>
      <div class="menu-navbar">
        <el-button size="small" type="" @click="cancelSubmit">取消</el-button>
        <el-button size="small" type="primary" :loading="isloading" @click="createSubmit('appendlist')">保 存</el-button>
      </div>
      <div>
        <el-form :model="appendlist" label-position="top" size="small" ref="appendlist" :rules="appendrules" label-width="110px">
          <el-row :gutter="20">
            <el-col :span="12">
              <el-form-item label="子系统名称" prop="name">
                <el-input v-model="appendlist.name" placeholder="子系统名称"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="子系统名称英文" prop="nameEn">
                <el-input v-model="appendlist.nameEn" placeholder="子系统名称英文"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="20">
            <el-col :span="12">
              <el-form-item label="子系统访问URL" prop="url">
                <el-input v-model="appendlist.url" placeholder="子系统访问URL"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="子系统编号" prop="code">
                <el-input v-model="appendlist.code" type="tel" placeholder="子系统编号"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="20">
            <el-col :span="12">
              <el-form-item label="是否显示在框架内" prop="isInner">

                <el-select v-model="appendlist.isInner" clearable placeholder="请选择" :disabled="false">
                  <el-option
                    v-for="item in isOrNo"
                      :key="item.type"
                      :label="item.value"
                      :value="item.type">
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="排序" prop="orderCode">
                <el-input v-model="appendlist.orderCode" type="tel" placeholder="排序"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="20">
            <el-col :span="12">
              <el-form-item label="图标" prop="icon">
                <el-input v-model="appendlist.icon" placeholder="图标"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </div>
    </div>
  </div>
</template>
<script>
  export default {
    data() {
      var nameEnglish = (rule, value, callback) => {
        if (/[\u4E00-\u9FA5]/g.test(value)) {
          callback(new Error('子系统名称英文不能包含中文'))
        } else {
          callback()
        }
      }
      return {
        isloading: false,
        classify: '',
        classifyOptions: [],
        appendlist: {
          name: '',
          nameEn: '',
          code: '',
          orderCode: '',
          url: '',
          icon: ''
        },
        appendrules: {
          name: [
            { message: '子系统名称不能为空', required: true, trigger: 'blur' },
            { min: 1, max: 128, message: '长度在 1 到 128 个字符', trigger: 'blur' }
          ],
          nameEn: [
            { message: '子系统名称英文不能为空', required: true, trigger: 'blur' },
            { required: true, validator: nameEnglish, trigger: 'blur' },
            { min: 1, max: 128, message: '长度在 1 到 128 个字符', trigger: 'blur' }
          ],
          url: [
            { message: '子系统访问URL不能为空', required: true, trigger: 'blur' },
            { min: 1, max: 256, message: '长度在 1 到 256 个字符', trigger: 'blur' }
          ],
          code: [
            { message: '子系统编号不能为空', required: true, trigger: 'blur' },
            { min: 1, max: 32, message: '长度在 1 到 32 个字符', trigger: 'blur' }
          ],
          orderCode: [
            { message: '排序不能为空', required: true, trigger: 'blur' },
            { min: 1, max: 4, message: '长度在 1 到 4 个字符', trigger: 'blur' }
          ],
          isInner: [
            { message: '是否显示在框架内不能为空', required: true, trigger: 'blur' }
          ]
        },
        isOrNo: [
          {
            type: '1',
            value: '是'
          },
          {
            type: '0',
            value: '否'
          }
        ]
      }
    },
    methods: {
      appendData() {
        this.$store.dispatch('SnbsysInsert', this.appendlist).then(res => {
          this.isloading = false
          this.$message({
            type: 'success',
            message: '添加成功!'
          })
          this.$emit('changeCloseDialog')
        }).catch((err) => {
          console.log(err)
        })
      },
      createSubmit(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            this.isloading = true
            // console.log(this.appendlist)
            this.appendData()
          } else {
            this.$message({
              type: 'danger',
              message: '添加失败!'
            })
            console.log('error submit!!')
            return false
          }
        })
      },
      cancelSubmit() {
        this.$emit('changeCloseDialog')
      }
    }
  }
</script>
<style scoped>
  .menu-navbar{
    padding:0 0 20px;
    text-align: right;
  }
  .container{
    padding: 20px;
    height: 100%;
    overflow-x: hidden;
    overflow-y: auto;
  }
</style>
